package weifei.live.simulation.im.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import weifei.live.simulation.entity.GroupLiveStreamVideoTimeCache;
import weifei.live.simulation.im.GroupLiveStream;
import weifei.live.simulation.im.service.GroupLiveService;
import weifei.live.simulation.service.GroupService;
import weifei.live.simulation.service.UserService;
import weifei.web.Result;

import java.util.Date;


@RestController
@RequestMapping("/im/live/group")
public class GroupLiveController
{
    @Autowired
    private UserService userService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private GroupLiveService groupLiveService;


    @Autowired
    private GroupLiveStreamVideoTimeCache dao_group_live_stream_video_time_cache;



    @PostMapping("/openLiveStream")
    public Result openLiveStream(HttpServletRequest request, @Valid @RequestBody GroupLiveStream groupLiveStream)
    {
        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        // 看 登录用户 是否为 群聊群主
        if (!this.groupLiveService.isOwner(user_id, groupLiveStream.getGroupId()))
            return Result.failed("没有权限");

        this.groupLiveService.addLiveStreamManager(groupLiveStream);
        return Result.success("直播间开启成功");
    }


    @PostMapping("/closeLiveStream")
    public Result closeLiveStream(HttpServletRequest request, @RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        // 看 登录用户 是否为 群聊群主
        if (!this.groupLiveService.isOwner(user_id, groupId))
            return Result.failed("没有权限");

        this.groupLiveService.removeLiveStreamManager(groupId);
        return Result.success("直播间关闭成功");
    }


    @PostMapping("/addLiveStreamVideoTimeCache")
    public Result addLiveStreamVideoTimeCache(
            HttpServletRequest request,
            @Valid @RequestBody GroupLiveStreamVideoTimeCache videoTimeCache
            )
    {
        var group_id = videoTimeCache.getGroupId();
        var start_time = videoTimeCache.getStartTime();
        var end_time = videoTimeCache.getEndTime();

        if (start_time.before(new Date()))
            return Result.failed("开始时间早于现在");
        if (start_time.after(end_time))
            return Result.failed("开始时间晚于结束时间");
        if (start_time.equals(end_time))
            return Result.failed("开始时间与结束时间相同");

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);
        // 看 登录用户 是否为 群聊群主
        if (!this.groupLiveService.isOwner(user_id, group_id))
            return Result.failed("没有权限");

        if (this.groupLiveService.getLiveStreamVideoTimeCacheOverTime(group_id, start_time) != null)
            return Result.failed("该开始时间已有任务");
        if (this.groupLiveService.getLiveStreamVideoTimeCacheOverTime(group_id, end_time) != null)
            return Result.failed("该结束时间已有任务");

        if (!videoTimeCache.insert())
            return Result.exception("添加失败，数据库出错");

        return Result.success("添加成功");
    }


    @PostMapping("/deleteLiveStreamVideoTimeCache")
    public Result deleteLiveStreamVideoTimeCache(
            HttpServletRequest request,
            @RequestParam("id") Integer cacheId
        )
    {
        if (cacheId == null)
            return Result.exception("参数错误");

        var token = request.getHeader("token");
        var user_id = this.userService.getUserIDByToken(token);

        QueryWrapper<GroupLiveStreamVideoTimeCache> qw_group_live_stream_video_time_cache = new QueryWrapper<>();
        qw_group_live_stream_video_time_cache.eq(GroupLiveStreamVideoTimeCache.Column.ID.getName(), cacheId);
        var cache = this.dao_group_live_stream_video_time_cache.selectOne(qw_group_live_stream_video_time_cache);
        if (cache == null)
            return Result.failed("不存在的任务");

        // 看 登录用户 是否为 群聊群主
        if (!this.groupLiveService.isOwner(user_id, cache.getGroupId()))
            return Result.failed("没有权限");

        if (!cache.deleteById())
            return Result.exception("删除失败，数据库出错");
        return Result.success("删除成功");
    }



    // 下面的没有登录拦截

    @GetMapping("/getLiveStreamState")
    @PostMapping("/getLiveStreamState")
    public Result getLiveStreamState(HttpServletRequest request, @RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");

        if (this.groupService.getGroupByID(groupId) == null)
            return Result.failed("不存在的群聊");

        var live_manager = this.groupLiveService.getLiveStreamManagerByGroupID(groupId);
        if (live_manager == null)
            return Result.success("直播间未开启", GroupLiveStream.State.Closed);
        return Result.success("直播间正在进行直播", GroupLiveStream.State.Living);
    }


    @GetMapping("/getAllLiveStreamVideoTimeCache")
    @PostMapping("/getAllLiveStreamVideoTimeCache")
    public Result getAllLiveStreamVideoTimeCache(@RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");
        if (this.groupService.getGroupByID(groupId) == null)
            return Result.failed("不存在的群聊");

        var list = this.groupLiveService.getAllLiveStreamVideoTimeCache(groupId);
        return Result.success("获取成功", list);
    }


    @GetMapping("/getLiveStreamVideoTimeCache")
    @PostMapping("/getLiveStreamVideoTimeCache")
    public Result getLiveStreamVideoTimeCache(@RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");
        if (this.groupService.getGroupByID(groupId) == null)
            return Result.failed("不存在的群聊");

        var cache = this.groupLiveService.getLiveStreamVideoTimeCache(groupId);
        return Result.success("获取成功", cache);
    }


    @GetMapping("/getLiveStreamVideoURL")
    @PostMapping("/getLiveStreamVideoURL")
    public Result getLiveStreamVideoURL(@RequestParam("id") Integer groupId)
    {
        if (groupId == null)
            return Result.exception("参数错误");
        if (this.groupService.getGroupByID(groupId) == null)
            return Result.failed("不存在的群聊");
        var url = this.groupLiveService.getLiveStreamVideoURLByGroupID(groupId);
        return Result.success("获取成功", url);
    }
}


